﻿using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using Api.Model;

namespace RssProxyService
{
    public class FlickerRssMapper : AbstractRssMapper
    {
        public override Article MapSingleArticle(SyndicationItem syndicationItem)
        {
            Article article = new Article();

            article.Title = syndicationItem.Title.Text;
            article.PublishDate = syndicationItem.PublishDate.DateTime;
            article.UpdateDate = syndicationItem.LastUpdatedTime.DateTime;
            article.Tags = new[] { new Tag() { Name = "Rss" }, new Tag() { Name = "Flicker" } };
            
            Item image = new Item();
            image.Type = "image";

            var imageLink = syndicationItem.Links
                                           .Where(link => link.RelationshipType == "enclosure")
                                           .FirstOrDefault();

            image.Value = imageLink.Uri.ToString();
            article.MainImageUri = image.Value;

            List<Item> items = new List<Item>();
            items.Add(image);

            article.Items = items;
            return article;
        }
    }
}
